package com.jf.cloud.service.sysmanager.common.db;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * TODO   特殊的 查询条件
 * @author ouyang 创建时间：2012-03-12 01:25:11
 */
public class Condition implements Serializable {
	private static final long serialVersionUID = 1L;

	static Log log = LogFactory.getLog(Condition.class);
	
	private String columnName; //  键值 对应数据库 字段名称
	private Object value; //  具体的参数值
	private String group; //  参数的组 程序自动兼容 组的查询 eg: and (a=2 or c=2 or d=2)
	private FindType type; // 相同的分组 or 查询 应具有相同的 组名  

	public Condition(String columnName, Object value) {
		this.columnName = columnName;
		this.value = value;
	}
	public Condition() {
		super();
	}

	/**
	 * TODO  添加 字段前缀
	 */
	public void addPrefix() {
		String prefix = FindType.flagMap.get(type.name() + "_");
		this.columnName = prefix + this.columnName;
	}

	/**
	* 公开方法 指定查询的类型-----------------------------------------------------------
	* TODO  
	* @author ouyang 创建时间：2012-03-12 02:07:37
	*/
	public void like() {
		this.type = FindType.LIKE;
		addPrefix();
	}

	public void more() {
		this.type = FindType.MORE;
		addPrefix();
	}

	public void less() {
		this.type = FindType.LESS;
		addPrefix();
	}

	public void in() {
		this.type = FindType.IN;
		addPrefix();
	}
	/**
	 * 按 % 符号位置区分 在左边
	 * @author ouyang 创建时间：Mar 31, 2012 10:36:57 AM
	 */
	public void likeLeft() { 
		this.type = FindType.LIKE_LEFT;
		addPrefix();
	}
	public void likeRight() {
		this.type = FindType.LIKE_RIGHT;
		addPrefix();
	}

	public void notIn() {
		this.type = FindType.NOT_IN;
		addPrefix();
	}

	public void notNull() {
		this.type = FindType.NOT_NULL;
		addPrefix();
	}

	public void isNull() {
		this.type = FindType.IS_NULL;
		addPrefix();
	}

	public void notEqual() {
		this.type = FindType.NOT_EQUAL;
		addPrefix();
	}

	public void moreEqual() {
		this.type = FindType.MORE_EQUAL;
		addPrefix();
	}

	public void lessEqual() {
		this.type = FindType.LESS_EQUAL;
		addPrefix();
	}
   // ------ 默认的 or 查询 子选项为 =
	public void or(String groupName) {
		this.type = FindType.OR;
		this.group = groupName;
		if(groupName!=null){
			this.columnName=groupName+FindType.OR_FLAG+this.columnName;
		}else {
			throw new RuntimeException("分组名称不允许 为空");
		}
	}
	 // ------ 默认的 or 查询 子选项为  like
	public void orLike(String groupName) {
		this.type = FindType.OR;
		this.group = groupName;
		if(groupName!=null){
			this.columnName=groupName+FindType.LIKE_OR_FLAG+this.columnName;
		}else {
			throw new RuntimeException("分组名称不允许 为空");
		}
		
	}
	//-----------------------------------------------------------------------
	public Object getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getGroup() {
		return group;
	}
    // 设置分组
	public void setGroup(String groupName) {
		this.group = groupName;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public FindType getType() {
		return type;
	}

	public void setType(FindType type) {
		this.type = type;
	}
}
